package com.superman.supermarket.client.composite.cart.invalid;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.widget.dialog.XConfirm;
import com.superman.mvpframe.client.widget.dialog.XConfirm.ConfirmCallBack;
import com.superman.supermarket.client.composite.IUIListModule;

/**
 * 失效商品列表模块
 * 
 * @author superman
 * @version 2018年8月9日上午11:33:46
 */
public class InvalidSkuListModule extends Composite implements IUIListModule {

	interface InvalidSkuListModuleUiBinder extends UiBinder<Widget, InvalidSkuListModule> {
	}

	private static InvalidSkuListModuleUiBinder uiBinder = GWT.create(InvalidSkuListModuleUiBinder.class);

	@UiField
	HTML invalidhtml;
	@UiField
	FlowPanel listpanel;

	private List<String> spus = new ArrayList<String>();

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月9日 上午11:33:48
	 */
	public InvalidSkuListModule() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("cleaninvalidbtn")
	void cleanInvalidClick(ClickEvent event) {
		XConfirm.confirm("确认清空失效商品?", new ConfirmCallBack() {

			@Override
			public void cancel() {

			}

			@Override
			public void ok() {
				cleanInvalidSkListu();
			}
		});
	}

	private void cleanInvalidSkListu() {
		XGinjector.INSTANCE.getPostMan().post("cart/invalid/clear", null, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("已清空失效商品"));
				InvalidSkuListModule.this.removeFromParent();
			}
		});
	}

	@Override
	public List<Map<String, Object>> getListData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setListData(List<Map<String, Object>> list) {
		invalidhtml.setText("失效商品" + list.size() + "件");

		listpanel.clear();
		for (Map<String, Object> map : list) {
			CartInvalidSkuModule cartInvalidSkuModule = new CartInvalidSkuModule();
			cartInvalidSkuModule.setData(map);
			listpanel.add(cartInvalidSkuModule);

			String spuId = String.valueOf(map.get("SPU_ID"));
			spus.add(spuId);
		}
	}

}
